package com.hjm.project.agentInfo.agentActActive.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.hjm.project.ActActive.ActActive.domain.ActActive;
import com.hjm.project.ActActive.ActActive.service.IActActiveService;
import com.hjm.project.ActStage.ActStage.service.IActStageService;
import com.hjm.project.actTransactionCompliance.compliance.service.IActTransactionComplianceService;
import com.hjm.project.agentInfo.agentActActive.domain.AgentActActive;
import com.hjm.project.agentInfo.agentActActive.service.IAgentActActiveService;
import com.hjm.project.agentInfo.agentActActive.utils.AgentPriceCommon;
import com.hjm.project.agentInfo.agentPolicy.domain.AgentPolicy;
import com.hjm.project.agentInfo.agentPolicy.service.IAgentPolicyService;
import com.hjm.project.retainRewards.domain.RetainRewards;
import com.hjm.project.retainRewards.service.IRetainRewardsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import javax.validation.Valid;

import com.hjm.framework.web.page.TableDataInfo;

/**
 * 代理政策Controller
 * 
 * @author maker-platform
 * @date 2024-03-21
 */
@Controller
@RequestMapping("/agentInfo/agentActActive")
public class AgentActActiveController extends BaseController {
    private String prefix = "agentInfo/agentActActive";

    @Resource
    private IAgentActActiveService agentActActiveService;
    @Resource
    private IActActiveService actActiveService;
    @Resource
    private IActStageService actStageService;
    @Resource
    private IAgentPolicyService agentPolicyService;
    @Resource
    private IRetainRewardsService retainRewardsService;
    @Resource
    private IActTransactionComplianceService actTransactionComplianceService;
    @RequiresPermissions("agentInfo:agentActActive:view")
    @GetMapping()
    public String agentActActive(String agentNum,ModelMap map) {
        map.put("agentNum",agentNum);
        return prefix + "/agentActActive";
    }

    /**
     * 查询代理政策列表
     */
    @RequiresPermissions("agentInfo:agentActActive:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgentActActive agentActActive) {
        startPage();
        List<AgentActActive> list = agentActActiveService.selectagentActActiveList(agentActActive);
        return getDataTable(list);
    }

    /**
     * 导出代理政策列表
     */
    @RequiresPermissions("agentInfo:agentActActive:export")
    @Log(title = "代理政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgentActActive agentActActive) {
        List<AgentActActive> list = agentActActiveService.selectagentActActiveList(agentActActive);
        ExcelUtil<AgentActActive> util = new ExcelUtil<AgentActActive>(AgentActActive.class);
        return util.exportExcel(list, "agentActActive");
    }

    /**
     * 新增代理政策
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增保存代理政策
     */
    @RequiresPermissions("agentInfo:agentActActive:add")
    @Log(title = "代理政策", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AgentActActive agentActActive) {
        return toAjax(agentActActiveService.insertagentActActive(agentActActive));
    }

    /**
     * 修改代理政策
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        AgentActActive agentActActive = agentActActiveService.selectagentActActiveById(id);
        mmap.put("agentActActive", agentActActive);
        return prefix + "/edit";
    }

    /**
     * 修改保存代理政策
     */
    @RequiresPermissions("agentInfo:agentActActive:edit")
    @Log(title = "代理政策", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgentActActive agentActActive) {
        return toAjax(agentActActiveService.updateagentActActive(agentActActive));
    }

    /**
     * 删除代理政策
     */
    @RequiresPermissions("agentInfo:agentActActive:remove")
    @Log(title = "代理政策", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(agentActActiveService.deleteagentActActiveByIds(ids));
    }

    /**
     * 设置结算
     */
    @GetMapping("/editPrice")
    public String editPrice(Integer id,ModelMap map) {
        AgentActActive agentActActive = new AgentActActive();
        agentActActive.setId(id);
        AgentActActive at = agentActActiveService.selectagentActActiveOne(agentActActive);
        map.put("agentNum",at.getAgentNum());
        map.put("actId",at.getActId());

        AgentPolicy agentPolicy = new AgentPolicy();
        agentPolicy.setActId(at.getActId());
        agentPolicy.setAgentNum(at.getAgentNum());
        List<AgentPolicy> agentPolicies = agentPolicyService.selectAgentPolicyList(agentPolicy);
        AgentPriceCommon agentPriceCommon = AgentPriceCommon.dbToView(agentPolicies);
        map.put("price",agentPriceCommon);
        return prefix + "/editPrice";
    }

    /**
     * 设置留存
     */
    @GetMapping("/editLc")
    public String editLc(Integer actId,String agentNum,ModelMap map) throws Exception {
        //获取活动信息
        //获取活动得服务费，流量费金额
        ActActive actActive = actActiveService.selectActActiveById(String.valueOf(actId));

        String llf_money = actActive.getSimFeeReturn();
        String llf_lc_money = actActive.getSim_fee_keep();
        String my_llf_lc_money = "";
        //流量费
        RetainRewards result = retainRewardsService.selectRetainRewardsBy(actId, agentNum, "1");
        if (result != null) {
            my_llf_lc_money = result.getRetainedValue().toString();
        }

        String fwf_money = actActive.getServiceFeeReturn();
        String fwf_lc_money = actActive.getService_fee_keep();
        String my_fwf_lc_money = "";

        //服务费
        result = retainRewardsService.selectRetainRewardsBy(actId, agentNum, "2");
        if (result != null) {
            my_fwf_lc_money = result.getRetainedValue().toString();
        }

        //激活
        String jh_money = "";
        String jh_lc_money = "";
        String my_jh_lc_money = "";

        //达标
        String db_money = "";
        String db_lc_money = "";
        String my_db_lc_money = "";
        //阶段信息
        List<Map<String,String>> stageList = new ArrayList<>();
        Map params = new HashMap<>();
        params.put("act_id",actId);
        List<Map<String, Object>> actTransactionCompliance = actTransactionComplianceService.getActTransactionCompliance(params);
        for (Map<String, Object> data : actTransactionCompliance) {
            Map<String, Object> stageParams = new HashMap<>();
            stageParams.put("stage_id",data.get("STAGE_ID"));
            List<Map<String, Object>> actStage = actStageService.getActStageList(stageParams);
            for (Map<String, Object> objectMap : actStage) {
                Integer stage = Integer.parseInt(objectMap.get("STAGE").toString());
                String matching_num = objectMap.get("MATCHING_NUM").toString();
                String keep_money = objectMap.get("KEEP_MONEY").toString();
                //激活
                if (stage == 1) {
                    jh_money = matching_num;
                    jh_lc_money = keep_money;
                    result = retainRewardsService.selectRetainRewardsByStage(actId, agentNum, "3",stage.toString());
                    if (result != null) {
                        my_jh_lc_money = result.getRetainedValue().toString();
                    }
                } else if (stage == 2){
                    //达标
                    db_money = matching_num;
                    db_lc_money =  keep_money;
                    result = retainRewardsService.selectRetainRewardsByStage(actId, agentNum, "4",stage.toString());
                    if (result != null) {
                        my_db_lc_money = result.getRetainedValue().toString();
                    }
                } else if (stage > 2){

                    //阶段
                    Map<String,String> dbData = new HashMap<>();
                    dbData.put("stage",stage.toString());
                    dbData.put("jd_money",matching_num);
                    dbData.put("jd_lc_money",keep_money);
                    dbData.put("my_jd_lc_money","");

                    result = retainRewardsService.selectRetainRewardsByStage(actId, agentNum, "5",stage.toString());
                    if (result != null) {
                        dbData.put("my_jd_lc_money",result.getRetainedValue().toString());
                    }
                    stageList.add(dbData);
                }
            }
        }
        map.put("agentNum",agentNum);
        map.put("actId",actId);
        map.put("llf_money",llf_money == null ? "" : llf_money);
        map.put("llf_lc_money",llf_lc_money == null ? "" : llf_lc_money);
        map.put("my_llf_lc_money",my_llf_lc_money == null ? "" : my_llf_lc_money);
        map.put("fwf_money",fwf_money == null ? "" : fwf_money);
        map.put("fwf_lc_money",fwf_lc_money == null ? "" : fwf_lc_money);
        map.put("my_fwf_lc_money",my_fwf_lc_money == null ? "" : my_fwf_lc_money);
        map.put("jh_money",jh_money == null ? "" : jh_money);
        map.put("jh_lc_money",jh_lc_money == null ? "" : jh_lc_money);
        map.put("my_jh_lc_money",my_jh_lc_money == null ? "" : my_jh_lc_money);
        map.put("db_money",db_money == null ? "" : db_money);
        map.put("db_lc_money",db_lc_money == null ? "" : db_lc_money);
        map.put("my_db_lc_money",my_db_lc_money == null ? "" : my_db_lc_money);
        map.put("stageList",stageList);
        return prefix + "/editLc";
    }


    /**
     * 设置结算
     */
    @Log(title = "设置结算", businessType = BusinessType.INSERT)
    @PostMapping( "/saveAgentPrice")
    @ResponseBody
    public AjaxResult saveAgentPrice(@Validated AgentPriceCommon agentPriceCommon) {
        agentActActiveService.vliAgentPrice(agentPriceCommon);
        agentPolicyService.saveAgentPrice(agentPriceCommon);
        return success();
    }


    /**
     * 设置留存
     */
    @Log(title = "设置留存", businessType = BusinessType.INSERT)
    @PostMapping( "/saveLc")
    @ResponseBody
    public AjaxResult saveLc(@RequestBody JSONObject body) {
        AjaxResult ajaxResult = retainRewardsService.insertRetainRewardsAgent(body);
        return ajaxResult;
    }
}
